package com.wb.oldwomen;

import android.view.MotionEvent;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.wb.fw.GateActivity;
import com.wb.fw.Global;
import com.wb.fw.IConstants;
import com.wb.fw.SoundManager;
import com.wb.fw.Util;
import com.wiyun.engine.nodes.Button;
import com.wiyun.engine.nodes.ColorLayer;
import com.wiyun.engine.nodes.Label;
import com.wiyun.engine.nodes.Sprite;

public class UIShop extends ColorLayer implements Ioldwomam {

	public static UIShop make(GameScene parent) {
		return new UIShop(parent);
	}
	GameScene mparent;
	private Label mscore;
//	Button manimSwitch;
	Sprite mshowWoman, mshowGougou;
	public UIShop(GameScene parent) {
		mparent = parent;
		setKeyEnabled(true);
		setTouchEnabled(true);
		
		Sprite bg = Sprite.make(R.drawable.tool_panel);
		bg.setScale(Global.sScaleRate);
		bg.autoRelease();
		bg.setPosition(Global.sWYSize.width/2, Global.sWYSize.height/2);
		addChild(bg);
		
		Button btnBack = Button.make(R.drawable.btn_back_n, R.drawable.btn_back_p, this, "xlback");
		btnBack.setScale(Global.sScaleRate);
		btnBack.autoRelease();
		btnBack.setAnchor(0, 1);
		btnBack.setPosition(Global.sStartX, Global.sStartY-9*Global.sScaleRate);
		addChild(btnBack);
		
		mscore = Label.make("", 35);
		mscore.autoRelease();
		mscore.setScale(Global.sScaleRate);
		mscore.setAnchor(1f, 0.5f);
		mscore.setPosition(Global.sEndX-40*Global.sScaleRate, Global.sStartY-46*Global.sScaleRate);//760,422
		addChild(mscore);
		mscore.setText(Integer.toString(Util.getGold(GateActivity.sInstance)));
		
		mshowWoman = Sprite.make(R.drawable.anim_select_woman);
		mshowWoman.autoRelease();
		mshowWoman.setAnchor(0.5f, 0f);
		mshowWoman.setScale(Global.sScaleRate);
		mshowWoman.setPosition(Global.sWYSize.width/2, Global.sEndY);
		mshowWoman.setVisible(false);
		addChild(mshowWoman);
		
		mshowGougou = Sprite.make(R.drawable.anim_select_gougou);
		mshowGougou.autoRelease();
		mshowGougou.setAnchor(0.5f, 0f);
		mshowGougou.setScale(Global.sScaleRate);
		mshowGougou.setPosition(Global.sWYSize.width/2, Global.sEndY);
		mshowGougou.setVisible(false);
		addChild(mshowGougou);
		
		Button btnswitch = Button.make(R.drawable.btn_switch_n, R.drawable.btn_switch_p, this, "xlswitch");
		btnswitch.setScale(Global.sScaleRate);
		btnswitch.setAnchor(0f, 0f);
		btnswitch.autoRelease();
		btnswitch.setPosition(Global.sStartX, Global.sEndY);
		addChild(btnswitch);
		///////////////////////////////
	}
	
	public void xlswitch() {
		SoundManager.clickMenuItem();
		int animType = Util.getAnimType(GateActivity.sInstance);
		if (animType == ANIM_TYPE_GOUGOU) {
			animType = ANIM_TYPE_WOMAN;
		} else {
			if (Util.isUnlockGougou(GateActivity.sInstance)) {
				animType = ANIM_TYPE_GOUGOU;
			} else {
				GateActivity.sInstance.runOnUiThread(new Runnable() {
					@Override
					public void run() {
						Toast.makeText(GateActivity.sInstance, "需要解锁全部关卡，才能使用小狗！",
								Toast.LENGTH_SHORT).show();	
					}
				});
			}
		}
		Util.saveAnimType(GateActivity.sInstance, animType);
		if (animType == ANIM_TYPE_GOUGOU) {
			mshowWoman.setVisible(false);
			mshowGougou.setVisible(true);
		} else if (animType == ANIM_TYPE_WOMAN) {
			mshowWoman.setVisible(true);
			mshowGougou.setVisible(false);
		}
	}
	
	public void xlback() {
		SoundManager.clickMenuItem();
		mparent.switchState(GS_GAME_LEVEL_SELECT);
	}
	
	public void enterShop() {
		int animType = Util.getAnimType(GateActivity.sInstance);
		if (animType == ANIM_TYPE_GOUGOU) {
			mshowWoman.setVisible(false);
			mshowGougou.setVisible(true);
		} else if (animType == ANIM_TYPE_WOMAN) {
			mshowWoman.setVisible(true);
			mshowGougou.setVisible(false);
		}
	}
	
	@Override
	public boolean wyTouchesBegan(MotionEvent event) {
		return true;
	}
	
	@Override
	protected boolean onBackButton() {
		if (isVisible()) {
			mparent.switchState(GS_GAME_LEVEL_SELECT);
		}
		return true;
	}
	//////////////////////////
}
